package com.zb.select.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zb.entity.*;
import com.zb.result.PageUtil;
import com.zb.select.service.OrderSelecService;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author WEN
 * @Date 2023/12/28 0028
 **/
@Service
public class OrderSelecServiceImpl implements OrderSelecService {
    @Resource
    private ZjWorkSetService workSetService;
    @Resource
    private ZjSpareService spareService;
    @Resource
    private ZjSpareImageService imageService;
    @Resource
    private ZjProductBoomService productBoomService;
    @Resource
    private ZjSpareSpecInfoService specInfoService;

    // 分页获取工单配件下拉数据列表
    @Override
    public Map<String, Object> getOrderSparePageSelect(Long custAccId, Long workId, SelectVo selectVo) {
        boolean showRelationSpare = workSetService.isShowRelationSpare(custAccId);
        PageUtil.setSelectPageLimit(selectVo);
        if (!showRelationSpare) {
            return spareService.pageSelect(custAccId, selectVo);
        }
        List<Long> spareIds = productBoomService.getSpareIdsPageSelect(workId, selectVo);
        if (CollectionUtil.isEmpty(spareIds)) {
            return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), 0, new ArrayList<>());
        }
        QueryWrapper<ZjSpare> spareQueryWrapper = new QueryWrapper<>();
        spareQueryWrapper.select("id", "name", "model", "code", "brand_id", "brand", "type_id", "type");
        spareQueryWrapper.in("id", spareIds);
        List<ZjSpare> spareList = spareService.list(spareQueryWrapper);
        // 获取备件图片
        List<ZjSpareImage> infos = imageService.getImageBySpareIds(spareIds);
        List<SelectGoodsVo> voList = spareList.stream().map(spare -> {
            SelectGoodsVo vo = new SelectGoodsVo(spare.getId(), spare.getName(),
                    infos.stream().filter(info -> Objects.equals(spare.getId(), info.getSpareId())).map(ZjSpareImage::getImage).findFirst().orElse(""));
            vo.setModel(spare.getModel());
            vo.setItemNumber(spare.getItemNumber());
            vo.setCode(spare.getCode());
            vo.setBrandId(spare.getBrandId());
            vo.setBrand(spare.getBrand());
            vo.setTypeId(spare.getTypeId());
            vo.setType(spare.getType());
            return vo;
        }).toList();
        return PageUtil.pageList(selectVo.getPage(), selectVo.getLimit(), productBoomService.getSpareIdsPageSelectCount(workId, selectVo), voList);
    }

    // 分页获取工单配件规格下拉数据列表
    @Override
    public Map<String, Object> getOrderSpareSpecSelect(Long custAccId, Long spareId, SelectVo selectVo) {
        boolean showRelationSpare = workSetService.isShowRelationSpare(custAccId);
        if (!showRelationSpare) {
            return specInfoService.pageSelect(spareId, selectVo);
        }
        return specInfoService.getOrderSpareSpecSelect(spareId, selectVo);
    }
}
